package vip.zhenzicheng.rocketmq.controller;

import com.zzc.zfast.web.domain.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import vip.zhenzicheng.rocketmq.domain.User;
import vip.zhenzicheng.rocketmq.rocketmq.RocketMQProducer;

/**
 * @author zhenzicheng
 * @date 2022-05-28 19:35
 */
@RestController
@RequestMapping("/producer")
@Slf4j
@Tag(name = "RocketMQ消息发送", description = "测试")
public class TestController {

  @Value("${rocketmq.producer.topic.test-topic}")
  private String topic;

  @Autowired
  private RocketMQProducer producer;

  @Operation(description = "简单消息")
  @PostMapping("/simpleMsg")
  public Result<String> sendMessage(String message) {
    producer.sendMessage(topic, message);
    return Result.msgOf("消息发送完成");
  }

  @Operation(description = "发送简单实体")
  @PostMapping("/normalModel")
  public Result<String> normalModel(@Parameter(description = "user") @RequestBody User user) {
    String receive = producer.sendAndReceive(topic + ":ZZC", user);
    log.info(receive);
    return Result.msgOf(receive);
  }

  // 这个发送事务消息的例子中有很多问题，需要注意下。
  @PostMapping("/sendTransactionMessage")
  public String sendTransactionMessage(String message) throws InterruptedException {
    producer.sendMessageInTransaction(topic, message);
    return "消息发送完成";
  }
}